package com.yax.mickey.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yax.mickey.common.constant.UserConstant;
import com.yax.mickey.common.exception.AccountNotFoundException;
import com.yax.mickey.common.exception.PasswordErrorException;
import com.yax.mickey.entity.User;
import com.yax.mickey.service.UserService;
import com.yax.mickey.mapper.UserMapper;
import com.yax.mickey.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author yax
* @description 针对表【user(用户信息表)】的数据库操作Service实现
* @createDate 2024-05-12 21:23:09
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        String username = user.getUsername();
        String password = user.getPassword();

        // 1、根据用户名查询数据库中的数据
        User user1 = userMapper.getByUsername(username);

        // 2、判断用户名是否存在
        if (user1 == null) {
            throw new AccountNotFoundException(UserConstant.ACCOUNT_NOT_FOUND);
        }

        // 3、判断密码是否正确
        if (!password.equals(user1.getPassword())) {
            throw new PasswordErrorException(UserConstant.PASSWORD_ERROR);
        }

        // 4、返回用户信息
        return user1;
    }

    @Override
    public User getUserByUsername(String username) {

        return userMapper.getUserByUsername(username);
    }

    @Override
    public void register(String username, String password) {
        // Md5加密密码
        String Md5String = Md5Util.md5(password);
        userMapper.register(username,Md5String);
    }

    @Override
    public Integer addUser(User user) {
        return userMapper.addUser(user);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

}




